<?php
namespace Swiftx\Bundle\WeChat\Factory;
use Swiftx\Bundle\WeChat\Interfaces\Common\BaseFactory;
use Swiftx\Bundle\WeChat\Model\Seller\Company as Entity;
use Swiftx\Interfaces\Httpd\Request;

/**
 * 管理员工厂实现类
 */
class Company extends BaseFactory {

    /**
     * 设置实体类型
     * @return string
     */
    public function setEntityClass(): string{
        return Entity::class;
    }

    /**
     * 根据用户名获取用户
     * @param string $email
     * @return Entity
     */
    public function fetchByEmail(string $email){
        $query = $this->query('Standard');
        $query->bind('email', $email);
        /** @var Entity $result */
        $result =  $query->fetch();
        return $result;
    }

    /**
     * 标记当前登陆用户
     * @param Request $request
     * @param Entity $user
     */
    public function registerLogin(Request $request, Entity $user){
        $session_name = $this->bundle->getName().'::company';
        $request->getSession()->set($session_name, $user->getID());
    }

    /**
     * 获取当前请求登陆用户
     * @param Request $request
     * @return Entity|null
     */
    public function currentLogin(Request $request){
        $session_name = $this->bundle->getName().'::company';
        $company_id = $request->getSession()->get($session_name);
        /** @var Entity $result */
        $result = $this->entityManager->fetch(Entity::class, $company_id);
        return $result;
    }


}